package main

import (
	"encoding/json"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"strings"
	"time"
)

func yiCheListHandle(src string, categoryId int, childId int, start int, end int) {
	var finish = false

	for i := start; i <= end; i++ {
		url := fmt.Sprintf(src, i)
		doc, err := createDoc(url)
		if err != nil {
			trace(fmt.Sprintf("%s createDoc failed : %v", url, err))
			continue
		}
		doc.Find(".article-card.horizon").Each(func(j int, s *goquery.Selection) {
			href, _ := s.Find(".figure").Attr("href")
			img, _ := s.Find(".figure img").Attr("src")

			article, _ := getArticle(href)
			if article.ArticleId > 0 {
				trace(fmt.Sprintf("易车 finish : %s", href))
				finish = true
			}

			if len(href) > 0 && finish == false {
				art := new(ClcwArticle)
				art.CategoryId = categoryId
				art.ChildId = childId
				art.ArticleFocus = img
				art.ArticleThumb = img
				art.FromUrl = href
				art.FromName = "易车"
				jsonStr, err := json.Marshal(art)
				if err != nil {
					trace(fmt.Sprintf("failed: %v", href))
					trace(fmt.Sprintf("json Marshal failed : %v", err))
				} else {
					trace(fmt.Sprintf("lpush : %v", href))
					lpush(conf.Redis.ArticleQueue, jsonStr)
				}
				time.Sleep(time.Duration(conf.Crawler.Interval) * time.Second)
			}
		})

		if finish == true {
			return
		}
	}
}

func yiCheDetailHandle(art *ClcwArticle) {
	urlArr := strings.Split(art.FromUrl, ".html")
	if len(urlArr) != 2 {
		return
	}
	url := urlArr[0] + "_all.html" + urlArr[1]

	doc, err := createDoc(url)
	if err != nil {
		trace(fmt.Sprintf("%s createDoc failed : %v", url, err))
		return
	}
	art.ArticleTitle = doc.Find(".tit-h1").Text()
	art.ArticleTitle = strReplace(art.ArticleTitle)

	art.Author = doc.Find(".article-information span:last-child").Text()
	art.Author = strings.Replace(art.Author, "来源：", "", -1)
	art.Author = strings.Replace(art.Author, "作者：", "", -1)

	//pushTime := doc.Find(".article-information").Contents().Eq(0).Text()
	//art.CreateTime, _ = formatTime(pushTime)

	art.CreateTime = int(time.Now().Unix())
	art.ArticleContent, _ = doc.Find(".article-content").Html()

	if len(art.ArticleTitle) <= 0 || len(art.ArticleContent) <= 0 {
		return
	}
	// art2utf8(art, "UTF-8")
	art.Status = 2
	art.Type = 2
	art.FromName = "易车"

	affected, err := engine.Table("clcw_article_crawler").Insert(art)
	if err != nil {
		trace(fmt.Sprintf("article insert failed: %v", err))
	} else {
		trace(fmt.Sprintf("article insert %d : %s", affected, art.ArticleTitle))
	}
}
